<template>
  <div class="user-management">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>用户管理</span>
        </div>
      </template>
      <div class="content-body">
       <el-button type="primary" @click="getList">搜索</el-button>
        <!-- 用户列表 -->
        <el-table :data="users" style="width: 100%; margin-top: 20px;">
          <!-- <el-table-column prop="userInfo.id" label="用户ID" width="280" /> -->
          <el-table-column prop="info.nickName" label="用户名" width="120" />
          <el-table-column prop="createTime" label="申请时间" width="180" />
          <el-table-column prop="info.age" label="年龄" width="80"/>
          <el-table-column prop="info.sexy" label="性别" width="80" align="center">
            <template #default="scope">
              {{ scope.row.info.sexy === 'male' ? '男' : '女' }}
            </template>
          </el-table-column>
          <el-table-column prop="info.phoneNo" label="手机号" width="120"/>
          <el-table-column prop="info.city" label="所在城市" width="120"/>
          <el-table-column prop="info.joinReason" label="加入原因" width="280"/>
          <el-table-column label="操作" width="150" fixed="right">
            <template #default="scope">
              <el-button type="primary" size="small" @click="handleEditUser(scope.row,'1')">同意</el-button>
              <el-button type="danger" size="small" @click="handleEditUser(scope.row,'2')">拒绝</el-button>
            </template>
          </el-table-column>
        </el-table>

      </div>
    </el-card>
  </div>
</template>

<script setup>
import { reactive, ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { Search } from '@element-plus/icons-vue'
import { getJoinList,updateUserStatus } from '@/request/user.js'


const total = ref(100)
const queryForm = reactive({
  userName: '',
  role: '',
  status: '',
  pageNum: 1,
  pageSize: 10
})
// 模拟用户数据
const users = ref([])
const getList = ()=>{
  getJoinList(queryForm).then(res=>{
    res.forEach(item => {
      item.info = JSON.parse(item.info)
    })
    console.log(res)
    users.value = res
    total.value = res.length
  })
}
getList();
// 同意或拒绝用户
const handleEditUser = (row,status) => {
  console.log('处理用户状态变更:', row, status)
  try {
    // 创建row的副本，避免直接修改原数据
    const updatedRow = { ...row }
    updatedRow.status = status
    updatedRow.info = JSON.stringify(updatedRow.info)
    ElMessageBox.confirm('确认操作吗？', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }).then(() => {
      console.log('发送请求更新用户状态:', updatedRow)
      updateUserStatus(updatedRow)
        .then(res => {
          console.log('更新成功:', res)
          ElMessage.success('操作成功')
          getList()
        })
        .catch(error => {
          console.error('更新用户状态失败:', error)
          ElMessage.error('操作失败：' + (error?.message || '未知错误'))
          // 刷新列表以恢复原始状态
          getList()
        })
    }).catch(() => {
      console.log('用户取消操作')
      // 用户取消操作时刷新列表，确保数据准确
      getList()
    })
  } catch (error) {
    console.error('处理过程中发生错误:', error)
    ElMessage.error('系统异常，请稍后重试')
  }
}

</script>

<style scoped>
.user-management {
  padding: 0px;
}

.content-body {
  padding-top: 10px;
}

.search-filter {
  display: flex;
  align-items: center;
}
</style>